<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <liujiqi@etrol.com> 
// +----------------------------------------------------------------------

namespace app\duty\controller;

use app\common\controller\Admin;
use app\duty\controller\api\Position as PositionApi;
use app\admin\controller\ajax\Library as Ajax;

/**
 * 值班管理
 * @package app\Position
 */
class Position  extends Admin

{
    
    protected $api;
    
	public function _initialize()
	{
		parent::_initialize();
		$this->api = new PositionApi();
            $this->ajax= new Ajax();
	}
    /**
     * 首页
     * 
     */
   	public function index() {
   	  if(IS_AJAX){
   	  	$positionList = $this->api->positionList();

   	  	return $positionList;
   	  }
      $type=$this->ajax->getDutyPositionType()->getData();
      $enterpriseList = model('basic/Enterprise')->column('fullName','id');
      $this->assign('enterpriseList',$enterpriseList);
      $this->assign('type',$type);
      return $this->fetch();
 	}

 	/**
 	 * 值班
 	 */
 	public function search($start=null,$end=null,$enterpriseId=null) {
 		if(IS_AJAX){
			$positionList = $this->api->search(input('start'),input('end'),input('enterpriseId'));
 			return $positionList;
 		}
 	}

  /**
   * 值班详情
   */
  public function detail($positionId) {
    if(IS_AJAX){
      $positionList = $this->api->search(input('positionId'));
      return $positionList;
    }
    return $this->fetch();
  }



    /**
     * 添加值班
     * 
     */
    public function add() {
   		 if(IS_POST){
   		 	$data = input('post.'); 
                  if($data['startDate']) {
                    $data['startDate']=strtotime($data['startDate']);
                  }
                  if ($data['endDate']) {
                    $data['endDate']=strtotime($data['endDate']);
                  }
                  return $this->api->add($data);
   		 }
           $type=$this->ajax->getDutyPositionType()->getData();
           $enterpriseList = model('basic/Enterprise')->column('fullName','id');
           $this->assign('enterpriseList',$enterpriseList);
            
           $this->assign('type',$type);
          
           return $this->fetch();    
     }
 
    /**
     * 修改值班
     * 
     */
    public function edit($positionId = null) {
      
    	  if(IS_POST){
    	  	$data = input('post.');

                 if($data['startDate']) {
                    $data['startDate']=strtotime($data['startDate']);
                  }
                  if ($data['endDate']) {
                    $data['endDate']=strtotime($data['endDate']);
                  }
    	  	return $this->api->edit($data,$positionId);
    	  }
     	  $positionList = $this->api->positionList($positionId)->getData();
        $type=$this->ajax->getDutyPositionType()->getData();
        $enterpriseList = model('basic/Enterprise')->column('fullName','id');
        $this->assign('enterpriseList',$enterpriseList);
        $this->assign('type',$type);
     	  $this->assign('info',$positionList['data'][0]);
      
         return $this->fetch();      
    }
    
    /**
     * 删除值班
     */
    public function del($positionId = null) {
    	if(IS_AJAX){
    		return $this->api->del($positionId);
    	}
    }
  }



